import QtQuick 2.0
import org.qtproject.qtquick 1.0

import "qrc:/js/resources/Constants.js" as Constant

import "../common"
import "../page"


Item {
    id: id_friendList

    width: parent.width;
    height: m_heightItem

    property int m_margin: 10 * m_ratio

    property int m_widthImage: m_heightItem - 1 * m_margin;
    property int m_heighImage: m_heightItem - 1 * m_margin;

    property var m_uid;   // kieu long
    property var m_fid: buddyId;
    property string m_displayName: displayName;
    property string m_avatarUrl: avatarUrl;
    property string m_messageStatus: messageStatus;
    property string m_avatarUrlLocal : avatarUrlLocal;
    property string m_postTime;


    property Component chatPage: ChatPage {
        m_friendId: m_fid
        m_userId: m_uid
    }

    signal heightItemChange(int heightItem);
    onHeightChanged: {
        heightItemChange(id_friendList.height)
    }

    opacity: 1

    Rectangle{
        anchors.fill: id_friendList
        color: "#ffffff"
    }

     Line {
         color: "#000000"
         width: parent.width - itemImage.width - m_margin
         x: itemImage.x + itemImage.width
     }

    Row {
        anchors.topMargin: m_margin
        anchors.leftMargin: m_margin
        anchors.bottomMargin: m_margin
        anchors.rightMargin: m_margin

        id: id_rowItem
        anchors.fill: id_friendList
        spacing: m_margin

        Item {
            id: itemImage
            width: m_widthImage
            height: m_heighImage
            anchors.verticalCenter: parent.verticalCenter

            Image {
                id: id_avatarUrl


                source: {
                    if(m_avatarUrlLocal ===""){
                        return m_avatarUrl
                    }else{
                        return "file://"+m_avatarUrlLocal;
                    }


                }

                height: m_widthImage
                width: m_heighImage

                visible: false

                onStatusChanged: {
                    if (id_avatarUrl.status == Image.Ready) {
                        id_avatarUrl.visible = true;
                        id_waitingImage.visible = false;
                    }
                }
            }

            WaitingImage {
                id: id_waitingImage
                width: m_widthImage
                height: m_heighImage

                visible: true;
            }

            ImageTranparent {
                m_width: m_widthImage
                m_height: m_heighImage
                color: "transparent"
            }
        }


        Column {
            spacing: m_margin


            Text {
                id: id_txtDisplayName
                text: m_displayName
                width: id_friendList.width - id_avatarUrl.width - 3 * m_margin
                font.family: "Arial"
                horizontalAlignment: Text.AlignJustify
                wrapMode: Text.WordWrap
                font.pixelSize: 20 * m_ratio;
            }
            Text {
                id: id_txtMessageStatus
                text: m_messageStatus
                width: id_friendList.width - id_avatarUrl.width - 3 * m_margin
                font.family: "Arial"
                font.italic: true
                verticalAlignment: Text.AlignBottom
                horizontalAlignment: Text.AlignJustify
                wrapMode: Text.WordWrap
                opacity: 0.5

                font.pixelSize: 18 * m_ratio
            }
        }
    }

    //Line { y: id_friendList.height; color: "#000000"}


    MouseArea {
        anchors.fill: id_friendList
        onClicked: {
            ApplicationInfo.onUpdatePrivateContactData(m_fid, m_uid);

            Constant.m_uidActive = m_uid;
            Constant.m_fidActive = m_fid;
            Constant.m_avatarUrlFriendActive = m_avatarUrl

            id_pageView.pop();
            id_pageView.push(Qt.resolvedUrl("qrc:/Qml/gui/page/ChatPage.qml"));
            //id_pageView.push(chatPage);
            console.log("FriendListDelegate.qml=========== friend id: " + m_fid + " uid : "+ m_uid);
        }
    }

}
